package com.fyl.oauth2.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fyl.oauth2.commons.ServerResponse;
import com.fyl.oauth2.domain.SOSUserDetails;
import com.fyl.oauth2.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component("loginSuccessHandler")
public class LoginSuccessHandler implements AuthenticationSuccessHandler {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ObjectMapper objectMapper;




    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        logger.info("登录成功2");
        SOSUserDetails user = (SOSUserDetails) authentication.getPrincipal();
        UserVo userVo = new UserVo();
        userVo.setUsername(user.getUsername());
        userVo.setAuthorities(user.getAuthorities().toArray());
        userVo.setOrles(user.user().getRoles());
        httpServletResponse.setContentType("application/json;charset=UTF-8");
        httpServletResponse.getWriter().write(objectMapper.writeValueAsString(
//                AjaxResponse.AjaxData(GlobalConfig.ResponseCode.SUCCESS.getCode(),
//                GlobalConfig.ResponseCode.SUCCESS.getDesc(),
                ServerResponse.createBySuccess("登录成功",userVo)
        ));
    }
}

